package com.daocao.hua.http;

import com.daocao.hua.model.ResponseModel;
import com.google.gson.Gson;

/**
 * Created by xsooy-pc on 2017/4/5.
 */

public class CallBackListener<T extends ResponseModel> implements ResponseListener {

    private final Class mClazz;
    private InfoCallBack<T> mCallBack;

    public CallBackListener(Class clazz,InfoCallBack<T> callBack) {
        mCallBack = callBack;
        mClazz = clazz;
    }

    @Override
    public void onStartInMainThread(Object result) {
        mCallBack.onFailure(result.toString());
    }

    @Override
    public void onFinishInMainThread(Object result) {

    }

    @Override
    public void onSuccessInMainThread(int statusCode, String content, Object result) {
        try {
            T model = (T) result;
                if (model.getStatus() !=0) {
                    mCallBack.onSuccess(model);
                } else {
                    mCallBack.onFailure(model.getMsg());
                }
            //返回数据接口
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object onSuccessInRequestThread(int statusCode, String content) {
        try {
            return new Gson().fromJson(content, mClazz);
        } catch (Exception e) {
            return null;
        }
    }
}
